﻿


namespace Test
 module Main = 

   open System
   open System.Windows
   open System.Windows.Controls
   open FsSignals.Signal
   open Path
   open Path1
   open StateMachine
   open Merge
   open Switch
   open Dynamic

   let mkMainWindow2 () = 
        let main = new Window()
        
        let rec engine () = 
                
                let lbdSource = sourceEvent main.MouseLeftButtonDown |> map (fun _ -> ())
                let lbuSource = sourceEvent main.MouseLeftButtonUp |> map (fun _ -> ())
            
                let rbdSource = sourceEvent main.MouseRightButtonDown |> map (fun _ -> ())
                let rbuSource = sourceEvent main.MouseRightButtonUp |> map (fun _ -> ())
            
                let disamb v = 
                        match v with
                        |Some (), _ -> true
                        |_, Some () -> false
                         
                let lb = merge lbdSource lbuSource |> map disamb
                let rb = merge rbdSource rbuSource |> map disamb
            
                let s1 = merge lb rb
            
                let s2 = holdPair s1
            
                let proc (l, r) =  
                                let proc' v =   match v with
                                                |true -> "down" 
                                                |false -> "up" 
                                printfn "Left: %A   Right: %A" (proc' l) (proc' r)
                                
                
                map proc s2
        engine ()    
        main
        
   let mkMainWindow4 () = 
        let main = new Window()
        
        let prt msg () =   printfn msg 
        let rec engine () = 

                let mldS = sourceEvent main.MouseLeftButtonDown |> map (fun _ -> ())
                

                let prt msg () =   printfn msg 
                let e() =   let mldS = sourceEvent main.MouseLeftButtonDown 
                            let s1 = loopSig  ["red"; "blue"; "green"] mldS |> map (fun msg -> printfn "%A" msg)
                            let mrdS = sourceEvent main.MouseRightButtonDown |> map (fun _ -> ())
                            let mrdS' = map  (prt "fin") mrdS
                            let s2 = mrdS' =>> engine
                            let s = switch s1 s2
                            s
                let mldS' = map (prt "début") mldS
                let s1 = wait (mldS =>> (fun () ->  dispose   mldS'
                                                    e()))
               
                s1

        engine ()    
        main

   let mkMainWindow6 () = 
        let main = new Window()
        let ff  _  = 
            let g path =  printfn "%A" path
            g
        let rec engine () = 
                
                let mldS = sourceEvent main.MouseLeftButtonDown 
                
                let prt msg () =   printfn msg 
                let e (arg:Input.MouseButtonEventArgs) = 
                            let mmdS = sourceEvent main.MouseMove 
                            let s1 = scan  (fun path (arg:Input.MouseEventArgs)  -> arg.GetPosition(main) :: path) [] mmdS 
                            let mrdS = sourceEvent main.MouseRightButtonDown |> map (fun _ -> ())
                            let s2 = sample s1 mrdS
                            let s3 = s2 //=>> (fun path -> printfn "%A" path)
                            switch s3 (s2 ==>> engine)
                let s1 = wait (mldS =>> e)
               
                s1
                 
        let s = engine ()  =>> (fun path -> printfn "%A" path)
        main

   let mkMainWindow7 () = 
        let main = new Window()
        let ff  _  = 
            let g path =  printfn "%A" path
            g
        let rec engine () = 
                
                let mldS = sourceEvent main.MouseLeftButtonDown 
                
                let prt msg () =   printfn msg 
                let e () = 
                            let mmdS = sourceEvent main.MouseMove 
                            let s1 = mmdS =>> (fun (arg:Input.MouseEventArgs) path ->   let pos = arg.GetPosition(main)
                                                                                        printfn "pos : %A" (pos::path)
                                                                                        pos::path)
                            let mrdS = sourceEvent main.MouseRightButtonDown 
                            let s2 = mrdS =>> (fun _ path ->    printfn "Fin\n" 
                                                                path)
                            let s3 = mergeExc s2 s1

                            let mldS = sourceEvent main.MouseLeftButtonDown 
                            let s4 = mldS =>> (fun _ path ->    printfn "Clean\n" 
                                                                [])
                            let s5 = mergeExc s3 s4

                            switch s5 (mrdS ==>> engine)
                let s1 = mldS --> (fun path ->  printfn "Start"
                                                path)                          
                let s2 = switch s1 (mldS ==>> e)
               
                s2
                 
        let s = engine () 
        stepAccumSig [] s
        main
        
   
   let f() = 
    let (t, c) = source()
    let ls = seq {for i in 0..10 -> (float)i }
    let a = t --> 2.0
    let i = integrateSig 0.0 0.0 a t
    i =>> (fun v -> printfn "%A" v)
    Seq.iter (fun i -> c i) ls

   let mkMainWindow () = 
        let main = new Window()
        //main.Content <- Path1.makePath()
        main.Content <- Path1Bis.makePath()
        //main.Content <- Path2.makePath()
//        main.Content <- Loops.makeIterColorPanel()
//        main.Content <- Loops.makeScanPanel()
 //       main.Content <- Safe.makeScanPanel()
//        main.Content <- NoGlicthes.makeNoGlitchPanel()
//       main.Content <- Switch1.makeSwitchPanel()
//        main.Content <- Wheel.makeWheelPanel()
//        main.Content <- Wheel.makeBoundedWheelPanel()
//        main.Content <- BAllOnTable.makeSwitchPanel()
        
        
        main     
        
   let main() = 
  
       // f()
        let culture =  System.Threading.Thread.CurrentThread.CurrentCulture // new CultureInfo( "fr", false )
        
        let app =  new Application() 
        let w = mkMainWindow()
        app.Run(w) |> ignore
        ()
 
  
  
   [<STAThread()>]
   main()
  
 (* 
    open System
    open Microsoft.FSharp.Control 
 
    let (es:Event<int>) = new Event<int>()
    let e = es.Publish    
    
    let f n v = printfn "%A %A" n v
    
    let d1 = e.Subscribe (f "hello")
    let d2 = e.Subscribe (f "bye")
    
    let e2 = Event.map (fun _ -> 3) e
    let e1 = Event.map (fun _ -> 6) e
    let e3 = Event.merge e2 e1 
    e3.Add(fun v -> printfn "%A" v)
  
    es.Trigger 6

    d1.Dispose()

  type Event<'T> =
 class
  new Event : unit -> Event<'T>
  member this.Trigger : 'T -> unit
  member this.Publish :  IEvent<'T>
 end
*)

(*    let mkMainWindow () = 
        let main = new Window()
        main.Title <- "Vi-Logo"
        
//        let mmHandler s e = ()
        let rec mmSource () = 
            let mmSource = sourceEvent main.MouseMove
            let proc (a:Input.MouseEventArgs) = printfn "%A\n" a.GetPosition
            let s1 = map proc mmSource
            let muSource = fromMouseButtonEvent main.MouseUp --> true
            map (fun _ -> printfn "Mouse Up") muSource
            switchSig s1 (muSource --> fun () -> mmSource.die()
                                                 muSource.die()
                                                 startSig())
        and startSig () = 
            let mdSource = fromMouseButtonEvent main.MouseDown --> true
            map (fun _ -> printfn "Mouse down") mdSource

            let s1 = (mdSource --> fun() -> mdSource.die()
                                            mmSource())
            let ns = nullSig()
            let r = switchSig ns  s1
            r
          
        startSig ()  
        main
*)